package com.foreknow.demo3;

/**
 * 冒泡排序
 *  已知数组
 *  规则：先进行一次排序的过程，然后重复
 *      [15 7 22 14 8 16 10]
 *
 *      [7 15 22 14 8 16 10]
 *      [7 15 22 14 8 16 10]
 *      [7 15 14 22 8 16 10]
 *      [7 15 14 8 22 16 10]
 *      [7 15 14 8 16 22 10]
 *      [7 15 14 8 16 10 |22(有序区)]
 *
 *      [7 15 14 8 16 10 |22(有序区)]
 *      [7 15 14 8 16 10 |22(有序区)]
 *      [7 14 15 8 16 10 |22(有序区)]
 *      [7 14 8 15 16 10 |22(有序区)]
 *      [7 14 8 15 10 | 16 22(有序区)]
 *
 *
 */
public class ArraySort {
    public static void main(String[] args) {
        //初始化数组
        int[] array = {15,7,22,14,8,16,10};
        for(int i = 1;i<array.length;i++) { //控制总的比较次数
            for(int j = 0;j<array.length-i;j++) { //相邻的两个元素进行比较
                if(array[j] > array[j+1]) {
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
        //遍历array数组
        for(int i = 0;i<array.length;i++){
            System.out.println(array[i]);
        }

    }
}
